﻿using Offy.Itdev.BuyIn.Intranet.Core.DTO;

namespace Offy.Itdev.BuyIn.Intranet.Services.Impl
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Contracts;
    using Core.Impl;
    using Core.Contracts;
    using ServiceLocator;
    using Microsoft.SharePoint;
    using Domains;
    using AutoMapper;

    public class ProductService : IProductService
    {
        private IServiceLocator Locator;

        public ProductService()
        {
            Locator = ServiceLocator.Instance;
            Locator.Register<IProductDAL>(new ProductDAL());
            Locator.Register<IProductCategoryDAL>(new ProductCategoryDAL());
        }

        public ProductService(SPWeb web)
        {
            Locator = ServiceLocator.Instance;
            Locator.Register<IProductDAL>(new ProductDAL(web));
            Locator.Register<IProductCategoryDAL>(new ProductCategoryDAL(web));
        }

        //public void GetProductList()
        //{
        //    var productDAL = Locator.Resolve<IProductDAL>();
        //    //var productCategoryDAL = Locator.Resolve<IProductCategoryDAL>();

        //    //IList<LookupDTO> lookupList = new List<LookupDTO>();
        //    //lookupList.Add(new LookupDTO() { ID = 1, Title = "" });
        //    //lookupList.Add(new LookupDTO() { ID = 2, Title = "" });

        //    //productDAL.AddProduct(new ProductDTO()
        //    //                      {
        //    //                          Title = "New testting",
        //    //                          Category = new LookupDTO() { ID = 1, Title = "" },
        //    //                          CategoryList = lookupList
        //    //                      });
        //    //productDAL.Get(1);
        //    productDAL.GetList();
        //}

        public IList<Product> GetProductList()
        {
            //Mapper.CreateMap<ProductDTO, Product>();
            Mapper.CreateMap<ProductDTO, Product>().ForMember(o => o.Category, m => m.Ignore());
            
            IList<Product> list = new List<Product>();
            var productDAL = Locator.Resolve<IProductDAL>();
            var categoryDAL = Locator.Resolve<IProductCategoryDAL>();
            var productList = productDAL.GetList();
            
            foreach (ProductDTO dto in productList)
            {
                var product = Mapper.Map<ProductDTO, Product>(dto);
                product.Category = categoryDAL.Get(dto.Category.ID);
                list.Add(product);
            }
            return list;
        }
    }
}
